home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / shutdown-fx-20-c / sfx init ƒ / code ƒ / sfx install.c < prev    next >
Text File  |  1994-07-11  |  4KB  |  119 lines

  1. /**********************************************************************\
  2.  
  3. File:        sfx install.c
  4.  
  5. Purpose:    This module handles installing shutdown procs and installing
  6.             and removing fades from the system heap.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "Shutdown.h"
  26. #include "sfx install.h"
  27. #include "sfx gestalt.h"
  28. #include "program globals.h"
  29.  
  30. short InstallTheShutdownProc(Str255 theName, short vRefNum, long dirID,
  31.     Boolean onShutdown, Boolean onRestart)
  32. /* assume: there are no shutdown procs currently installed */
  33. /* assume: sfx Gestalt function is properly installed */
  34. {
  35.     OSErr            theResError;
  36.     FSSpec            fs;
  37.     short            oldRefNum, newRefNum;
  38.     Boolean            alreadyOpen;
  39.     Handle            theProcHandle;
  40.     ProcPtr            theFade, theShutdownProc, theRestartProc;
  41.     unsigned long    theSize;
  42.     unsigned long    dummy;
  43.     short            resultCode;
  44.     
  45.     BlockMove(theName, fs.name, theName[0]+1);
  46.     fs.vRefNum=vRefNum;
  47.     fs.parID=dirID;
  48.     theResError=OpenTheResFile(&fs, &oldRefNum, &newRefNum, &alreadyOpen);
  49.     if (theResError!=noErr)
  50.         return kCantOpenModule;
  51.     
  52.     theProcHandle=Get1Resource('PROC', 0);
  53.     if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  54.         return kModuleDamaged;
  55.         
  56.     if (*theProcHandle==0L)
  57.         LoadResource(theProcHandle);
  58.     if (*theProcHandle==0L)
  59.         return kModuleDamaged;
  60.         
  61.     HLock(theProcHandle);
  62.     theFade=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  63.     BlockMove(*theProcHandle, theFade, theSize);
  64.     ReleaseResource(theProcHandle);
  65.     theProcHandle=0L;
  66.     CloseTheResFile(oldRefNum, newRefNum, alreadyOpen);
  67.     
  68.     theProcHandle=Get1Resource('PROC', 10);    /* get shutdown proc from sfx init */
  69.     if (((theResError=ResError())!=noErr) || (theProcHandle==0L))
  70.         return kCantGetProc10Resource;
  71.  
  72.     if (*theProcHandle==0L)
  73.         LoadResource(theProcHandle);
  74.     if (*theProcHandle==0L)
  75.     {
  76.         DisposePtr(theFade);
  77.         return kCantGetProc10Resource;
  78.     }
  79.     
  80.     theShutdownProc=NewPtrSys(theSize=GetHandleSize(theProcHandle));
  81.     theRestartProc=NewPtrSys(theSize);
  82.     BlockMove(*theProcHandle, theShutdownProc, theSize);
  83.     BlockMove(*theProcHandle, theRestartProc, theSize);
  84.     ReleaseResource(theProcHandle);
  85.     theProcHandle=0L;
  86.     
  87.     *(ProcPtr*)((long)theShutdownProc+6)=
  88.         *(ProcPtr*)((long)theRestartProc+6)=theFade;            /* store address of fade */
  89.     *(short*)((long)theShutdownProc+10)=onShutdown ? 1 : 0;        /* store on shutdown flag */
  90.     *(short*)((long)theRestartProc+10)=onRestart ? 1 : 0;        /* store on restart flag */
  91.     ShutDwnInstall(theShutdownProc, sdOnPowerOff);
  92.     ShutDwnInstall(theRestartProc, sdOnRestart);
  93.     resultCode=SetGestaltProcPtrs(theFade, theShutdownProc, theRestartProc);
  94.     
  95.     return resultCode;
  96. }
  97.  
  98. OSErr OpenTheResFile(FSSpec* fs, short* oldRefNum, short* newRefNum, short* alreadyOpen)
  99. {
  100.     unsigned long    oldTopMapHndl;
  101.     OSErr            theResError;
  102.     
  103.     *oldRefNum=CurResFile();
  104.     oldTopMapHndl=(unsigned long)TopMapHndl;
  105.     *newRefNum=HOpenResFile(fs->vRefNum, fs->parID, fs->name, fsRdPerm);
  106.     theResError=ResError();
  107.     *alreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
  108.     return theResError;
  109. }
  110.  
  111. void CloseTheResFile(short oldRefNum, short newRefNum, Boolean alreadyOpen)
  112. {
  113.     if (!alreadyOpen)
  114.     {
  115.         CloseResFile(newRefNum);
  116.     }
  117.     UseResFile(oldRefNum);
  118. }
  119.